package com.lib.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lib.common.constant.CreateUpdateConstant;
import com.lib.common.constant.StatusConstant;
import com.lib.mapper.CategoryMapper;
import com.lib.pojo.DTO.CategoryDTO;
import com.lib.pojo.DTO.PageDTO;
import com.lib.pojo.VO.CategoryVO;
import com.lib.pojo.entity.BookInformation;
import com.lib.pojo.entity.Category;
import com.lib.pojo.entity.PageResult;
import com.lib.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    //1：根据分类ID查询图书
    @Override
    public List<CategoryVO> getCategoryById(Integer categoryId) {
        if (categoryId == null){
            return categoryMapper.getCategoryByIdAll();
        }else {
            return categoryMapper.getCategoryById(categoryId);
        }
    }

    //2:根据id删除分类
    @Override
    public void delete(Integer categoryId) {
        categoryMapper.delete(categoryId);
    }

    //3:新增分类
    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        //创建一个分类对象
        Category category = Category.builder()
                .categoryName(categoryDTO.getCategoryName())
                .sort(categoryDTO.getSort())
                .status(StatusConstant.STATUS_DOWN)
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .createUser(CreateUpdateConstant.CREATE_USER)
                .updateUser(CreateUpdateConstant.UPDATE_USER)
                .build();
        categoryMapper.addCategory(category);
    }

    //4:启用、禁用分类
    @Override
    public void statusStartOrStop(Integer status, Integer categoryId) {
        //创建对象category
        Category category = Category.builder()
                .status(status)
                .categoryId(categoryId)
                .updateTime(LocalDateTime.now())
                .updateUser(CreateUpdateConstant.UPDATE_USER)
                .build();
        categoryMapper.putCategory(category);
    }

    //5：修改分类
    @Override
    public void putCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.putCategory(category);
    }

    //2：用户端-返回分类名称及ID
    @Override
    public List<Category> getCategoryName() {
        return  categoryMapper.getCategoryName();
    }

    //6：分类分页查询
    @Override
    public PageResult page(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPage(),pageDTO.getPageSize());
        if (pageDTO.getCategoryId() != null){
            //只查询指定分类下的图书
            Page<Category> page = categoryMapper.page(pageDTO);
            long total = page.getTotal();
            List<Category> records = page.getResult();
            return new PageResult(total,records);
        }
        //查询所有
        Page<Category> page = categoryMapper.pageAll(pageDTO);
        long total = page.getTotal();
        List<Category> records = page.getResult();
        return new PageResult(total,records);
    }

    //7：根据分类ID查询一个分类
    @Override
    public Category getById(Integer id) {
        return categoryMapper.getById(id);
    }

    //8：批量删除分类
    @Override
    public void deleteByIds(List<Integer> ids) {
        //判断这个分类关联的是否有图书
        for (Integer id : ids){
            List<BookInformation> bookInformations = categoryMapper.getByCategoryId(id);
            if (bookInformations.size() > 0){
                //有图书，不能删除
                throw new RuntimeException("该分类下有图书，不能删除");
            }
        }
        for (Integer id : ids){
            categoryMapper.delete(id);
        }
    }
}
